﻿@model NationalIT.Driver_Info
@{
    ViewBag.Title = "Driver Payroll & Expenses";
}
<script type="text/javascript">
    $(document).ready(function () {
        // trip
        ListenerCheckBox($("#TripAmountTable .DriverTripCheckAll"), $("#TripAmountTable .DriverTripCheckItem"), $("#TripsAmount"));
        //fuel expenses
        ListenerCheckBox($("#Fuel_ExpensesAmount .DriverFuel___ExpensesAll"), $("#Fuel_ExpensesAmount .DriverFuel___ExpensesItem"), $("#FuelAmount"));
        //operating expenses
        ListenerCheckBox($("#Operating_ExpensesAmount .DriverOperating_ExpensesAll"), $("#Operating_ExpensesAmount .DriverOperating_ExpensesItem"), $("#OperatingAmount"));
        //Driver split expenses
        ListenerCheckBox($("#DriverExpensesSplit .SplitcheckAll"), $("#DriverExpensesSplit .Splitcheckitem"), $("#DriversAmount"));
        ListenerCheckAll($("#DriverExpensesSplit .SplitcheckAll"), $("#DriverExpensesSplit .Splitcheckitem"));
        
        //Owner split expenses 
        ListenerCheckBox($("#OwnerExpensesSplit .SplitcheckAll"), $("#OwnerExpensesSplit .Splitcheckitem"), $("#OwnersAmount"));
        ListenerCheckAll($("#OwnerExpensesSplit .SplitcheckAll"), $("#OwnerExpensesSplit .Splitcheckitem"));
        
        //Escrow EscrowLoanAmount
        ListenerCheckBox($("#Escrow .EscrowcheckAll"), $("#Escrow .Escrowcheckitem"), $("#EscrowCurrentCharge"));
        ListenerCheckAll($("#Escrow .EscrowcheckAll"), $("#Escrow .Escrowcheckitem"));

        $(".PrintPayroll1").click(function () {
            if (!confirm("Are you sure you want process this Payroll?")) {
                return false;
            }
            var Trips = getListIDCheckBoxByIDClassName("TripAmountTable", "DriverTripCheckItem");
            var Fuel = getListIDCheckBoxByIDClassName("Fuel_ExpensesAmount", "DriverFuel___ExpensesItem");
            var operating = getListIDCheckBoxByIDClassName("Operating_ExpensesAmount", "DriverOperating_ExpensesItem");
            var splitdriver = getListIDCheckBoxByIDClassName("DriverExpensesSplit", "Splitcheckitem");
            var splitowner = getListIDCheckBoxByIDClassName("OwnerExpensesSplit", "Splitcheckitem");
            var escrowLoan = getListIDCheckBoxByIDClassName("Escrow", "Escrowcheckitem");
            var isescrowloan = "0";
            if ($("#EscrowCurrentCharge").val()!='$0.00') {
                isescrowloan = "1";
            }
            var driverid = '@Model.ID';
            var url = $(this).attr('href');
            var newurl = addParam(addParam(addParam(addParam(addParam(addParam(addParam(addParam(url, "isescrowloan", isescrowloan), "escrowLoan", escrowLoan), "driverid", driverid), "Trips", Trips), "Fuel", Fuel), "operating", operating), "splitdriver", splitdriver), "splitowner", splitowner);
            var beforeURL = addParam("@Url.Action("BeforeDriverPayroll", "DriverInfo")","url",newurl);
            window.open(beforeURL,'_blank');
            location.reload();
            return false;
        });
    });
    function ListenerCheckBox(objCheckAll, objCheckItem, TotalAmount) {
        objCheckAll.click(function () {
            if ($(this).is(':checked')) {
                var checkAll = 0;
                objCheckItem.each(function () {
                    checkAll = parseFloat(checkAll) + parseFloat($(this).val());
                });
                TotalAmount.val("$" + formatCurrency(checkAll));
            } else {
                TotalAmount.val("$0.00");
            }
        });
        objCheckItem.click(function () {
            var $this = $(this);
            var tripAmount = 0;            
            if ($this.is(':checked')) {
                var countChecked = 0;
                objCheckItem.each(function () {
                    if ($(this).is(":checked")) {
                        countChecked++;
                    }
                });
                if (countChecked == objCheckItem.length) {
                    objCheckAll.prop('checked', true);
                }
            } else {
                objCheckAll.prop('checked', false);
            }
            // Duyệt toàn bộ các checkbox được checked và lấy giá trị cộng dồn lại
            objCheckItem.each(function () {
                if ($(this).is(":checked")) {
                    tripAmount = parseFloat(tripAmount) + parseFloat($(this).val());                    
                }
            });
            TotalAmount.val("$" +formatCurrency(tripAmount));
        });
    }
   
    function ListenerCheckAll(checkAll,checkItem) {
        checkAll.click(function () {            
            if ($(this).is(':checked')) {
                checkItem.prop('checked', true);
            } else {
                checkItem.prop('checked', false);
            }
        });
    }
    function ListenerCheckItem(checkAll,checkItem) {
        checkItem.click(function () {            
            if ($(this).is(':checked')) {
                var isChecked = true;
                checkItem.each(function () {
                    if ($(this).is(":checked")==false) {
                        isCheck = false;
                    }
                });
                if (isChecked==true) {
                    checkAll.prop('checked', true);
                }else {
                    checkAll.prop('checked', false);
                }
            } else {
                checkAll.prop('checked', false);
            }
        });
    }
    function formatCurrency(num) {
        num = isNaN(num) || num === '' || num === null ? 0.00 : num;
        return parseFloat(num).toFixed(2).replace(/\d(?=(\d{3})+\.)/g, '$&,');
    }
    $(document).on("click", ".saveCurrentCharge", function () {
        var $this = $(this);
        var url = $this.attr('href');
        var currentCharge = $this.parent().prev().find(".currentCharge").val();
        var newURL = addParam(url, "currentCharge", currentCharge);
        $.ajax({
            type: "GET",
            url: newURL,
            success: function (model) {
                alert('Saved!');
                $this.parents("tr").find(".Escrowcheckitem").val(currentCharge);
            }
        });
        return false;
    });
</script>
<h3>
    Driver Payroll & Expenses: <a href="/DriverInfo/NewOrEdit/@Model.ID">@(Model.Last_name + " " + Model.First_name)</a></h3>
@{
    var db = NationalIT.DB.Entities;
    var lstFuel_Expenses = Model.Fuel___Expenses.Where(m => m.Current_Payroll == true && m.Paid_off == false).ToList();
    var lstOperating_Expenses = Model.Operating_Expenses.Where(m => m.Current_Payroll == true && m.Paid_off == false).ToList();
    var listTripInfoConditon = Model.Trip_Info.Where(m => m.Picked == true && m.Deliverd == true && m.Current_Payroll == true && m.Customer_Invoiced == true && m.Driver_paid == false).ToList();
    var driverExpenses = db.split_expenses.Where(m => m.Idndex == Model.ID && m.OwnerDriver == 2 && m.Paid_Off == false).ToList();
    var OwnerExpenses = db.split_expenses.Where(m => m.Idndex == Model.Owner_Name && m.OwnerDriver == 1 && m.Paid_Off == false).ToList();
    var escrowLoan = db.EscrowLoan.Where(m =>m.Balance > 0 &&  ((!m.Owner && m.OwnerDriver == Model.ID)
                || (Model.Owner_Name.HasValue && Model.Owner_Name.Value != 0 && m.Owner && m.OwnerDriver == Model.Owner_Name.Value))).ToList();
}
<div class="row-fluid">
    <div class="span8">
        <div class="box">
            <ul class="nav nav-tabs">
                <li class="active"><a href="#tab-1-1" data-toggle="tab">Trips</a></li>
                <li><a href="#tab-1-2" data-toggle="tab">Fuel Expenses</a></li>
                <li><a href="#tab-1-3" data-toggle="tab">Operating Expenses</a></li>
                <li><a href="#tab-1-4" data-toggle="tab">Split Expenses</a></li>
                <li><a href="#tab-1-5" data-toggle="tab">Escrow/loan</a></li>
            </ul>
            <div class="box-content">
                <div class="tab-content">
                    <div class="tab-pane active" id="tab-1-1">
                        @Html.Partial("/Views/Partial-Payrolls-RollBack/_IndexPartialTripInfo1.cshtml", listTripInfoConditon)
                    </div>
                    <div class="tab-pane" id="tab-1-2">
                        @Html.Partial("/Views/Partial-Payrolls-RollBack/_Fuel_ExpensesPartial1.cshtml", lstFuel_Expenses)
                    </div>
                    <div class="tab-pane" id="tab-1-3">
                        @Html.Partial("/Views/Partial-Payrolls-RollBack/_Operating_ExpensesPartial1.cshtml", lstOperating_Expenses)
                    </div>
                    <div class="tab-pane" id="tab-1-4">
                        <ul class="nav nav-tabs">
                            <li class="active"><a href="#tab-1-4-1" data-toggle="tab">Driver's expenses</a></li>
                            <li><a href="#tab-1-4-2" data-toggle="tab">Owner's expenses</a></li>
                        </ul>
                        <div class="box-content">
                            <div class="tab-content">
                                <div class="tab-pane active" id="tab-1-4-1">
                                    @Html.Partial("/Views/Partial-Payrolls-RollBack/_DriverExpenses.cshtml", driverExpenses)
                                </div>
                                <div class="tab-pane" id="tab-1-4-2">
                                    @Html.Partial("/Views/Partial-Payrolls-RollBack/_OwnerExpenses.cshtml", OwnerExpenses)
                                </div>
                            </div>
                        </div>
                    </div>
                    <div class="tab-pane" id="tab-1-5">
                        @*<ul class="nav nav-tabs">
                            <li class="active"><a href="#tab-1-5-1" data-toggle="tab">Escrow</a></li>
                            <li><a href="#tab-1-5-2" data-toggle="tab">Loan</a></li>
                        </ul>*@
                        <div class="box-content">
                            <div class="tab-content">
                            @Html.Partial("/Views/Partial-Payrolls-RollBack/_EscrowPartial.cshtml", escrowLoan)
                                @*<div class="tab-pane active" id="tab-1-5-1">
                                    @Html.Partial("/Views/Partial-Payrolls-RollBack/_EscrowPartial.cshtml", escrowLoan)
                                </div>@*
                                <div class="tab-pane" id="tab-1-5-2">
                                    @Html.Partial("/Views/Partial-Payrolls-RollBack/_LoanPartial.cshtml", loan)
                                </div>*@
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="span4">
        <div class="box">
            <div class="box-content">
                <div class="control-group">
                    <label class="control-label" for="TripsAmount">
                        Trips Amount:</label>
                    <div class="controls">
                        <div class="span12">
                            <input type="text" value="$0.00" name="TripsAmount" id="TripsAmount" class="input-large"
                                data-rule-number="true" readonly="readonly" />
                        </div>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="FuelAmount">
                        Fuel Amount:</label>
                    <div class="controls">
                        <div class="span12">
                            <input type="text" value="$0.00" name="FuelAmount" id="FuelAmount" class="input-large"
                                data-rule-number="true" readonly="readonly" />
                        </div>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="OperatingAmount">
                        Operating Amount:</label>
                    <div class="controls">
                        <div class="span12">
                            <input type="text" value="$0.00" name="OperatingAmount" id="OperatingAmount" class="input-large"
                                data-rule-number="true" readonly="readonly" />
                        </div>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="DriversAmount">
                        Drivers Amount:</label>
                    <div class="controls">
                        <div class="span12">
                            <input type="text" value="$0.00" name="DriversAmount" id="DriversAmount" class="input-large"
                                data-rule-number="true" readonly="readonly" />
                        </div>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="OwnersAmount">
                        Owners Amount:</label>
                    <div class="controls">
                        <div class="span12">
                            <input type="text" value="$0.00" name="OwnersAmount" id="OwnersAmount" class="input-large"
                                data-rule-number="true" readonly="readonly" />
                        </div>
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="EscrowCurrentCharge">
                        Total Escrow / Loan:</label>
                    <div class="controls">
                        <div class="span12">
                            <input type="text" value="$0.00" name="EscrowCurrentCharge" id="EscrowCurrentCharge"
                                class="input-large" data-rule-number="true" readonly="readonly"  />
                        </div>
                    </div>
                </div>
                @*<div class="control-group">
                    <label class="control-label" for="LoanCurrentCharge">
                        Loan (Current charge):</label>
                    <div class="controls">
                        <div class="span12">
                            <input type="text" value="0" name="LoanCurrentCharge" id="LoanCurrentCharge"
                                class="input-large" data-rule-number="true" /> ($)
                        </div>
                    </div>
                </div>*@
                <div class="control-group">
                    <label class="control-label" for="OwnersAmount">
                    </label>
                    <div class="controls">
                        <div class="span12">
                            <a href="" class="btn">Refresh</a>
                        </div>
                    </div>
                </div>
                <div class="form-actions">
                    <a href="/Reports/Payroll.aspx" class="btn btn-primary PrintPayroll1">Print Payroll</a>
                    <input type="submit" class="btn btn-primary" value="PDF Payroll">
                </div>
            </div>
        </div>
    </div>
</div>
